package com.lvshi0797.cms.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lvshi0797.cms.dao.IBaseInfoDao;
import com.lvshi0797.cms.dao.IDisabledInfoDao;
import com.lvshi0797.cms.dao.IMemberInfoDao;
import com.lvshi0797.cms.dto.MemberDto;
import com.lvshi0797.cms.entities.FDisabledInfo;
import com.lvshi0797.cms.entities.FMember;
import com.lvshi0797.cms.helper.BussinessException;

@Service("memberInfoService")
public class MemberInfoService implements IMemberInfoService {
	@Autowired
	private IBaseInfoDao baseInfoDao;
	@Autowired
	private IMemberInfoDao memberInfoDao;
	@Autowired
	private IDisabledInfoDao disabledInfoDao;

	@Override
	public MemberDto createArchive(MemberDto member) throws BussinessException {
		if (member == null||member.getMember()==null) {
			throw new BussinessException("请求数据异常，请稍后重新登录提交！");
		}
		FMember fmember = member.getMember();
		FDisabledInfo disabledInfo = member.getDisabledInfo();
		MemberDto memDto = new MemberDto();
		FDisabledInfo disabledInfo2 = null;
		if(fmember.getIsDisable()==1){//如果是残疾，就加入信息
			disabledInfo2 = disabledInfoDao.add(disabledInfo);
			fmember.setDisabledInfo(disabledInfo2);
		}else{
			fmember.setDisabledInfo(null);
		}
		FMember add2 = memberInfoDao.add(fmember);
		//返回FMember的ID
		memDto.setMemberId(add2.getId()+"");
		return memDto;
	}
	

	@Override
	public List<MemberDto> listMember(int id) {
		List<FMember> members = memberInfoDao.listMemberByBid(id);
		List<MemberDto> memberDtoList = new ArrayList<MemberDto>();
		//户主
		List<MemberDto> memberHuzhu = new ArrayList<MemberDto>();
		//残疾人
		List<MemberDto> memberDisa = new ArrayList<MemberDto>();
		//正常人
		List<MemberDto> memberNormal = new ArrayList<MemberDto>();
		MemberDto mdto = null;
		
		for(FMember m:members){
			mdto = new MemberDto();
			mdto.setMember(m);
			//如果是残疾，需要从数据库中读取出残疾证的相关信息
			if(m.getIsHuzhu()==1){//添加户主
				mdto.setItem("建档立卡户主情况");
				memberHuzhu.add(mdto);
			}else if(m.getIsDisable()==1){//残疾人
				mdto.setDisabledInfo(m.getDisabledInfo());
				mdto.setItem("该户残疾人基本信息");
				memberDisa.add(mdto);
			}else{
				mdto.setItem("除户主、残疾人外其他成员信息");
				memberNormal.add(mdto);
			}
		}
		memberDtoList.addAll(memberHuzhu);
		memberDtoList.addAll(memberDisa);
		memberDtoList.addAll(memberNormal);
		return memberDtoList;
	}

	@Override
	public void updateMember(MemberDto memberDto) throws BussinessException {
		if(memberDto==null){
			throw new BussinessException("业务实例不存在，请稍后重试登录再试！");
		}
		FMember member = memberDto.getMember();
		FDisabledInfo disabledInfo = memberDto.getDisabledInfo();
		if(member!=null){
			int memberid = member.getId();
			//数据库中的家庭成员的信息
			FMember dbMember = memberInfoDao.getById(memberid);
			//数据库中的DisabledInfo信息
			FDisabledInfo dbdisabledInfo = dbMember.getDisabledInfo();
			if(dbdisabledInfo==null){//原来数据库中没有残疾卡的信息
				//提交的信息中包含了需要维护这个卡信息，新增卡信息，更新member信息
				if(member.getIsDisable()==1&&disabledInfo!=null){
					disabledInfoDao.add(disabledInfo);
					member.setDisabledInfo(disabledInfo);
				}else{
					member.setDisabledInfo(null);
				}
			}else{//原来数据库中有了这个卡片的信息，则要删除这个信息
				int dbid = dbdisabledInfo.getId();
				disabledInfoDao.delete(dbid);
				if(member.getIsDisable()==1&&disabledInfo!=null){//要维护卡片信息
					disabledInfoDao.add(disabledInfo);
					member.setDisabledInfo(disabledInfo);
				}else{
					member.setDisabledInfo(null);
				}
			}
			memberInfoDao.update(member);
		}
	}

	@Override
	public MemberDto loadMember(int id) throws BussinessException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deleteMemberInfoById(int id) throws BussinessException {
		memberInfoDao.delete(id);
	}


	@Override
	public void updateMemberInfo(String memberId, String baseInfoId) {
		
		memberInfoDao.updateMember(Integer.parseInt(memberId), Integer.parseInt(baseInfoId));
	}

	 
	
 
}
